﻿using System.Data.SqlClient;
using System.Windows;
using System.Windows.Controls;

namespace 餐饮管理系统
{
    public partial class MainWindow : Window
    {
        private void Gongying_addButton_Click(object sender, System.Windows.RoutedEventArgs e)
        {

            ListBoxItem Supplier = CenterListBox.SelectedItem as ListBoxItem;
            if (Supplier == null)
                return;
            string SupplierName = Supplier.Content.ToString().Trim();
            string nowdate = System.DateTime.Now.ToString();
            string sql = "insert into SupplierOrderTable(date,supplier_name) "
                        + "values("
                        + "'" + nowdate + "'" + "," + "'" + SupplierName + "'"
                        + ")";
            databaseConnect.ExecuteQuery(sql);
            loadOrders(getOrderNumber(SupplierName) - 1);
        }
        private void loadOrders(int selectedIndex = 0)
        {
            ListBoxItem Supplier = CenterListBox.SelectedItem as ListBoxItem;

            if (Supplier == null)
            {
                return;
            }
            string SupplierName = Supplier.Content.ToString().Trim();
            string sql = "select id  from SupplierOrderTable where supplier_name = '" + SupplierName + "'";
            SqlDataReader dataReader = databaseConnect.GetDataReader(sql);
            GongyingListBox.Items.Clear();
            int count = 0;
            while (dataReader.Read())
            {
                count++;
                string orderInfo = string.Format("{0,3}", dataReader[0].ToString().Trim());
                string ischeck = string.Format("{0,3}", dataReader[0].ToString().Trim());
                ListBoxItem item = new ListBoxItem();
                item.SetResourceReference(ListBoxItem.StyleProperty, "ListBoxItemStyle1");
                item.Content = orderInfo;
                GongyingListBox.Items.Add(item);
                if (ischeck.Equals("1"))
                    OrderCheckButton.SetResourceReference(Button.StyleProperty, "");
            }
            dataReader.Close();

            if (count == 0)
            {
                SupplierOrderGrid.Visibility = Visibility.Hidden;
            }
            else
            {
                SupplierOrderGrid.Visibility = Visibility.Visible;
            }

            if (selectedIndex < 0 || selectedIndex >= GongyingListBox.Items.Count)
                selectedIndex = 0;

            if (this.GongyingListBox.Items.Count > 0)
            {
                (this.GongyingListBox.Items[selectedIndex] as ListBoxItem).IsSelected = true;
                GongyingListBox.ScrollIntoView(GongyingListBox.Items[selectedIndex]);
            }
        }
        private int getOrderNumber(string SupplierName)
        {
            string sql = "select count(id) from SupplierOrderTable where supplier_name = '" + SupplierName + "'";
            object buffer = databaseConnect.ExecuteScaler(sql);
            if (buffer == null)
            {
                return -1;
            }
            else
            {
                return (int)buffer;
            }
        }
        private int loadOrderSelected()
        {
            ListBoxItem order = GongyingListBox.SelectedItem as ListBoxItem;
            if (order == null)
            {
                OrderDateBox.Text = "";
                OrderNumberBox.Text = "";
                OrderCheckButton.Visibility = Visibility.Visible;
                return 0;
            }
            string orderInfo = order.Content.ToString().Trim();
            string id = orderInfo.Trim();

            string sql = "select * from SupplierOrderTable where id = '" + id + "'";
            SqlDataReader dataReader = databaseConnect.GetDataReader(sql);
            while (dataReader.Read())
            {
                OrderDateBox.Text = dataReader[1].ToString().Trim();
                OrderNumberBox.Text = dataReader[0].ToString().Trim();
                SupplierOrderBox.Text = dataReader[3].ToString().Trim();
                string ischeck = dataReader[2].ToString().Trim();
                if(ischeck.Equals("True"))
                    OrderCheckButton.Visibility = Visibility.Hidden;
                else if(ischeck.Equals("False"))
                    OrderCheckButton.Visibility = Visibility.Visible;
            }
            dataReader.Close();
            return 1;
        }
        private void OrderListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            loadOrderSelected();
        }
        private void OrderCheckButton_Click(object sender, RoutedEventArgs e)
        {
            int i=loadOrderSelected();
            if (i == 0)
                return;
            string sql = "update SupplierOrderTable "
                      + "set checked = " + 1
                      + "where id = " + OrderNumberBox.Text;
            databaseConnect.ExecuteQuery(sql);
            OrderCheckButton.Visibility = Visibility.Hidden;
            loadOrders(GongyingListBox.SelectedIndex);

        }
        private void Gongying_saveButton_Click(object sender, RoutedEventArgs e)
        {
           
            string sql = "update SupplierOrderTable "
                                  + "set goods = '" + SupplierOrderBox.Text + "' "
                                  + "where id = " + OrderNumberBox.Text;
            databaseConnect.ExecuteQuery(sql);
            loadOrders(GongyingListBox.SelectedIndex);
        }
        private void Gongying_deleteButton_Click(object sender, RoutedEventArgs e)
        {
            ListBoxItem order = GongyingListBox.SelectedItem as ListBoxItem;
            if (order == null)
            {
                return;
            }
            string orderInfo = order.Content.ToString().Trim();
            string id = orderInfo.Trim();
            string sql = "delete from SupplierOrderTable where id = " + id;
            databaseConnect.ExecuteQuery(sql);
            loadOrders(GongyingListBox.SelectedIndex);
            loadOrderSelected();
        }
    }
}
